home *** CD-ROM | disk | FTP | other *** search
- Path: unidhp.uni-c.dk!phower
- From: phower@unidhp.uni-c.dk (Peter Henriksen)
- Newsgroups: comp.lang.c++
- Subject: Core-dumps in g++ 2.7.0 when redefining new/delete
- Date: 21 Jan 1996 22:19:59 GMT
- Organization: News Server at UNI-C, Danish Computing Centre for Research and Education.
- Message-ID: <4due6f$5f1@news.uni-c.dk>
- NNTP-Posting-Host: unidhp.uni-c.dk
- Summary: Core-dumps in g++ 2.7.0 when redefining new/delete
- Keywords: new/delete
- X-Newsreader: TIN [version 1.2 PL1]
-
- Core-dumps in g++ 2.7.0 when redefining new/delete.
-
- We are having problems with GNU g++ 2.7.0 when redefining the new/delete operators.
- A code example is shown below. It core-dumps during the free call.
-
- We believe that the problem originates from the fact that the new operator is not
- returning the malloc address. To get this specific example to core-dump, we need
- filling the allocated space, but other (larger) examples will core-dump just by
- shifting the pointer returned by malloc.
-
- The compiler was taken from Slackware 3.0.0, and the op. sys. we are running is Linux
- 1.2.13. Other compilers (GNU g++ 2.6.3, SGI CC) work without problems.
-
- Any ideas why this is happening? Are we doing something wrong, or is there a bug in
- the compiler? Is there a change in the C++ standard that we are not aware of?
-
- Thanks in advance
- Peter
-
- PS : Please mail me directly (phower at unidhp.uni-c.dk)
-
-
- ////////////////////////////////////CODE EXAMPLE///////////////////////////////////
- #include <iostream.h>
- #include <strstream.h>
- #include <malloc.h>
-
- // Shift the malloc address 16 bytes. This way we should be certain that we do not
- // violate any alignment restrictions.
- #define EXTRA 16
-
- void * operator new ( size_t user_size ) {
- // allocate requested size + EXTRA
- size_t size = user_size + EXTRA;
- void* pointer = malloc(size);
-
- // fill complete space with 0xff
- char* destroy = (char*) pointer;
- for ( long i = 0; i < size; i++, destroy++ ) *destroy = 0xff;
-
- // return malloc pointer + EXTRA
- char* user_pointer = ((char*) pointer) + EXTRA;
- return user_pointer;
- }
- void operator delete ( void* dead_obj ) {
- // correct pointer so we free the same address we got from malloc
- char* user_pointer = ((char*) dead_obj) - EXTRA;
- free((void*) user_pointer);
- }
-
- // OStr just used to give us the core-dump. It has probably nothing to
- // do with the problem. In this example, a char[] is allocated and a
- // ostrstream is attached.
- class OStr {
- public:
- OStr(int size) {
- string_ = new char[size];
- string_stream_ = new ostrstream(string_,size);
- }
- ~OStr() {
- delete string_stream_;
- delete[] string_;
- }
-
- char* string_;
- ostrstream* string_stream_;
- };
-
-
- void main(int argc, char** argv) {
- static char str[] = "Something";
- istrstream* str_stream = new istrstream(str);
-
- // next line only to get the core-dump.
- OStr* new_string = new OStr(14);
-
- cout << "Before Delete" << flush;
- delete str_stream;
- cout << "... After Delete\n" << flush;
- }
-
-
-
-